//
//  CPSPerformanceView.m
//  CPSPerformanceSDK
//
//  Created by 胡中楷 on 2021/3/26.
//

#import "CPSPerformanceView.h"

#define screenW  [UIScreen mainScreen].bounds.size.width
#define screenH  [UIScreen mainScreen].bounds.size.height

@interface CPSPerformanceView ()

@property (nonatomic, strong) UILabel *labelFPS;
@property (nonatomic, strong) UILabel *labelMemory;
@property (nonatomic, strong) UILabel *labelCPU;
@property (nonatomic, strong) UILabel *labelNetworkFlow;

@property (nonatomic, assign) CGPoint beginPoint;

@property (nonatomic, assign) double firstMemory;
@property (nonatomic, assign) BOOL flag;

@end

@implementation CPSPerformanceView

- (void)hidePerformanceView{
    [self removeFromSuperview];
}

- (void)showPerformanceViewInView:(UIView *)view{
    if(view){
        self.userInteractionEnabled = YES;
        [view addSubview:self];
    }
}

- (void)cps_updateShowContentFPS:(double)fps memory:(double)memory cpu:(double)cpu networkFlow:(NSString *)networkFlow{
    if(self.labelFPS.superview){
        self.labelFPS.text = [NSString stringWithFormat:@"FPS:%.1lf",fps];
        if (fps < 40) {
            self.labelFPS.textColor = UIColor.redColor;
        }else if (fps >= 50){
            self.labelFPS.textColor = UIColor.greenColor;
        }else {
            self.labelFPS.textColor = UIColor.yellowColor;
        }
    }
    if(self.labelMemory.superview){
        
        if (!self.flag) {
            self.firstMemory = memory;
            self.flag = !self.flag;
        }
        
        self.labelMemory.text = [NSString stringWithFormat:@"内存:%.1lfMB",memory];
        if (memory <= self.firstMemory*2) {
            self.labelMemory.textColor = UIColor.greenColor;
        }else if (memory > self.firstMemory*3){
            self.labelMemory.textColor = UIColor.redColor;
        }else{
            self.labelMemory.textColor = UIColor.yellowColor;
        }
    }
    if(self.labelCPU.superview){
        self.labelCPU.text = [NSString stringWithFormat:@"CPU:%.1lf%%",cpu];
        if (cpu <= 30) {
            self.labelCPU.textColor = UIColor.greenColor;
        }else if (cpu >= 60){
            self.labelCPU.textColor = UIColor.redColor;
        }else{
            self.labelCPU.textColor = UIColor.yellowColor;
        }
    }
    if(self.labelNetworkFlow.superview){
        self.labelNetworkFlow.text = [NSString stringWithFormat:@"流量:%@",networkFlow];
        if ([networkFlow containsString:@"B/s"]) {
            self.labelNetworkFlow.textColor = UIColor.greenColor;
        }else if ([networkFlow containsString:@"KB/s"]){
            NSArray *arrayFlow = [networkFlow componentsSeparatedByString:@"K"];
            if ([arrayFlow[0] integerValue] >= 500) {
                self.labelNetworkFlow.textColor = UIColor.redColor;
            }else if ([arrayFlow[0] integerValue] <= 250){
                self.labelNetworkFlow.textColor = UIColor.greenColor;
            }else{
                self.labelNetworkFlow.textColor = UIColor.yellowColor;
            }
        }else{
            self.labelNetworkFlow.textColor = UIColor.redColor;
        }
    }
}

- (UILabel *)labelFPS{
    if(!_labelFPS){
        UILabel *labelFPS = [[UILabel alloc]initWithFrame:self.bounds];
        labelFPS.tag = 20210326;
        labelFPS.font = [UIFont systemFontOfSize:15.0f];
        labelFPS.textColor = [UIColor whiteColor];
        labelFPS.backgroundColor = UIColor.clearColor;
        labelFPS.numberOfLines = 0;
        [self addSubview:labelFPS];
        _labelFPS = labelFPS;
    }
    return _labelFPS;
}

- (UILabel *)labelMemory{
    if(!_labelMemory){
        UILabel *labelMemory = [[UILabel alloc]initWithFrame:self.bounds];
        labelMemory.tag = 20210327;
        labelMemory.font = [UIFont systemFontOfSize:15.0f];
        labelMemory.textColor = [UIColor whiteColor];
        labelMemory.backgroundColor = UIColor.clearColor;
        labelMemory.numberOfLines = 0;
        [self addSubview:labelMemory];
        _labelMemory = labelMemory;
    }
    return _labelMemory;
}

- (UILabel *)labelCPU{
    if(!_labelCPU){
        UILabel *labelCPU = [[UILabel alloc]initWithFrame:self.bounds];
        labelCPU.tag = 20210328;
        labelCPU.font = [UIFont systemFontOfSize:15.0f];
        labelCPU.textColor = [UIColor whiteColor];
        labelCPU.backgroundColor = UIColor.clearColor;
        labelCPU.numberOfLines = 0;
        [self addSubview:labelCPU];
        _labelCPU = labelCPU;
    }
    return _labelCPU;
}

- (UILabel *)labelNetworkFlow{
    if(!_labelNetworkFlow){
        UILabel *labelNetworkFlow = [[UILabel alloc]initWithFrame:self.bounds];
        labelNetworkFlow.tag = 20210329;
        labelNetworkFlow.text = @"0KB/s";
        labelNetworkFlow.font = [UIFont systemFontOfSize:15.0f];
        labelNetworkFlow.textColor = [UIColor whiteColor];
        labelNetworkFlow.backgroundColor = UIColor.clearColor;
        labelNetworkFlow.numberOfLines = 0;
        [self addSubview:labelNetworkFlow];
        _labelNetworkFlow = labelNetworkFlow;
    }
    return _labelNetworkFlow;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    
    self.layer.cornerRadius = 5;
    self.layer.masksToBounds = YES;
    
    //添加拖拽手势-改变控件的位置
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(changePostion:)];
    [self addGestureRecognizer:pan];
    
    self.backgroundColor = [UIColor.blackColor colorWithAlphaComponent:0.5];
    
    if(_labelFPS){
        _labelFPS.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height/4);
    }
    if(_labelMemory){
        _labelMemory.frame = CGRectMake(0, self.frame.size.height/4, self.frame.size.width, self.frame.size.height/4);
    }
    if(_labelCPU){
        _labelCPU.frame = CGRectMake(0, self.frame.size.height/4*2, self.frame.size.width, self.frame.size.height/4);
    }
    if(_labelNetworkFlow){
        _labelNetworkFlow.frame = CGRectMake(0, self.frame.size.height/4*3, self.frame.size.width, self.frame.size.height/4);
    }
}

- (void)changePostion:(UIPanGestureRecognizer *)pan{
    CGPoint point = [pan translationInView:self];
    
    CGRect originalFrame = self.frame;
    
    originalFrame = [self changeXWithFrame:originalFrame point:point];
    originalFrame = [self changeYWithFrame:originalFrame point:point];
    
    self.frame = originalFrame;
    
    [pan setTranslation:CGPointZero inView:self];
    
    UIButton *button = (UIButton *)pan.view;
    if (pan.state == UIGestureRecognizerStateBegan) {
        button.enabled = NO;
    }else if (pan.state == UIGestureRecognizerStateChanged){
    } else {
        
        CGRect frame = self.frame;
        
        if (self.center.x <= screenW / 2.0){
            frame.origin.x = 0;
        }else
        {
            frame.origin.x = screenW - frame.size.width;
        }
        
        if (frame.origin.y < 20) {
            frame.origin.y = 20;
        } else if (frame.origin.y + frame.size.height > screenH) {
            frame.origin.y = screenH - frame.size.height;
        }
        
        [UIView animateWithDuration:0.3 animations:^{
            self.frame = frame;
        }];
        
        button.enabled = YES;
        
    }
}

//拖动改变控件的水平方向x值
- (CGRect)changeXWithFrame:(CGRect)originalFrame point:(CGPoint)point{
    BOOL q1 = originalFrame.origin.x >= 0;
    BOOL q2 = originalFrame.origin.x + originalFrame.size.width <= screenW;

    if (q1 && q2) {
        originalFrame.origin.x += point.x;
    }
    return originalFrame;
}

//拖动改变控件的竖直方向y值
- (CGRect)changeYWithFrame:(CGRect)originalFrame point:(CGPoint)point{

    BOOL q1 = originalFrame.origin.y >= 20;
    BOOL q2 = originalFrame.origin.y + originalFrame.size.height <= screenH;
    if (q1 && q2) {
        originalFrame.origin.y += point.y;
    }
    return originalFrame;
}

@end
